//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     彩种业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Enum.Common;
using CL.Game.DAL;
using CL.Game.Entity;
using CL.Json.Entity.WebAPI;
using CL.Redis.BLL;
using CL.Tools.Common;
using System.Collections.Generic;
using System.Linq;

namespace CL.Game.BLL
{

    /// <summary>
    /// LotteriesBLL 业务
    /// </summary>
    public class LotteriesBLL
    {
        LotteriesDAL dal = new LotteriesDAL(DbConnectionEnum.CaileGame);
        /// <summary>
        /// 查询所有可用彩种
        /// </summary>
        /// <returns></returns>
        public List<LotteriesEntity> QueryLotterys()
        {
            return dal.QueryLotterys();
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public LotteriesEntity QueryLotterys(int LotteryID)
        {
            return dal.Get(new { LotteryID = LotteryID }, "LotteryID desc");
        }
        public LotteriesEntity QueryEntity(int LotteryCode)
        {
            return dal.Get(new { LotteryCode = LotteryCode }, "LotteryID desc");
        }
        /// <summary>
        /// 插入彩种对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertEntity(LotteriesEntity entity)
        {
            return dal.InsertEntity(entity);
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool ModifyEntity(LotteriesEntity entity)
        {
            return dal.Update(entity) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int LotteryID)
        {
            return dal.Exists(LotteryID);
        }
        /// <summary>
        /// 是否存在该彩种编码
        /// </summary>
        /// <param name="LotteryCode"></param>
        /// <returns></returns>
        public bool ExistsCode(int LotteryCode)
        {
            return dal.ExistsCode(LotteryCode);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<LotteriesEntity> QueryEntitys(string strWhere)
        {
            return dal.QueryEntitys(strWhere);
        }
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="LotteryID"></param>
        /// <returns></returns>
        public bool DelEntity(int LotteryID)
        {
            return dal.DelEntity(LotteryID);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<LotteriesEntity> QueryListByPage(string strName, string orderby, int pageSize, int pageIndex, ref int recordCount)
        {
            return dal.QueryListByPage(strName, orderby, pageSize, pageIndex, ref recordCount);
        }
        #region 自定义方法
        /// <summary>
        /// 系统彩种信息
        /// 局部使用本地缓存 初始化加载提升效率
        /// </summary>
        /// <returns></returns>
        public List<LotteryData> SystemLottyData()
        {
            string CacheKey = "Cache_SystemLottyData";
            List<LotteryData> result = CacheHelper.Get<List<LotteryData>>(CacheKey);
            if (result == null || result.Count == 0)
            {
                result = new SystemRedis().ApplyLotteryDataRedis();
                if (result == null || result.Count == 0)
                {
                    //官方加奖
                    List<int> awards = new ActivityAwardBLL().QueryAwardLotteryCode(0, 0);
                    //系统加奖
                    List<int> caile_Awards = new ActivityAwardBLL().QueryAwardLotteryCode(1, 0);
                    result = new List<LotteryData>();
                    List<LotteriesEntity> ls = this.QueryLotterys();

                    foreach (var item in ls)
                    {
                        bool IsAward = false;
                        bool Awards = false;
                        if (awards.Contains(item.LotteryCode))
                        {
                            IsAward = true;
                            item.IsAddaward = true;
                        }
                        if (caile_Awards.Contains(item.LotteryCode))
                            item.IsAddaward = true;
                        if (item.IsAddaward && IsAward)
                            Awards = true;
                        result.Add(new LotteryData()
                        {
                            ChatGroups = item.ChatGroups,
                            ChatRooms = item.ChatRooms,
                            IsHot = item.IsHot ?? false,
                            AddawardSubhead = "",
                            Subhead = item.Subhead,
                            LotteryCode = item.LotteryCode,
                            LotteryIconUrl = "",
                            LotteryName = item.LotteryName,
                            //1今日开奖,2加奖,3奖上奖,4暂停销售
                            LotteryStatus = item.IsStop == true ? 4 : item.IsAddaward == true ? 2 : Addaward.IsToDayLottery(item.LotteryCode, item.IntervalType.Split('@')[0].Split(',')) == true ? 1 : Awards == true ? 3 : 0 //今日开奖 奖上奖
                        });
                    }
                    new SystemRedis().ApplyLotteryDataRedis(result);
                }
                if (result != null && result.Count > 0)
                    result = result.OrderBy(o => o.LotteryStatus).ToList();
                //本地缓存
                CacheHelper.Insert(CacheKey, result, 10);
            }
            return result;
        }

        #endregion
    }
}
